<?php
// In application.ini: autoloaderNamespaces[] = "Ahs"
/**
 * Abstracte klasse voor getters en setters in een Model
 *
 * @category   Ahs
 * @package    Ahs_Model
 * @author     Olivier Parent
 * @copyright  Copyright (c) 2011 Artevelde University College Ghent
 */
abstract class Ahs_Model
{

    public function __construct(array $options = null)
    {
        if (is_array($options)) {
            $this->_setOptions($options);
        }
    }

    public function __get($name)
    {
        $method_name = "get{$name}";
        if ( $this->_isMethod($method_name) ) {
            return $this->$method_name();
        }
    }

    public function __set($name, $value)
    {
        $method_name = "set{$name}";
        if ( $this->_isMethod($method_name) ) {
            $this->$method_name($value);
        }
    }

    /**
     * @param string $method_name
     * @return boolean 
     */
    protected function _isMethod($method_name)
    {
        if (!method_exists($this, $method_name)) {
            throw new Exception('Invalid ' . get_class() . ' property');
        }

        return true;
    }

    /**
     * @param array $options
     */
    protected function _setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method_name = 'set' . ucfirst($key);
            if ( in_array($method_name, $methods) ) {
                $this->$method_name($value);
            }
        }
    }
    
}
